package com.lesson14.observe;

import java.util.ArrayList;

/**
 * @author 朝花不迟暮
 * @version 1.0
 * @date 2020/9/17 20:35
 */
public class WeatherData implements Subject
{
    private float temperature;
    private float pressure;
    private float humidity;

    private ArrayList<Observer> observers;

    public WeatherData()
    {
        observers = new ArrayList<>();
    }

    public float getTemperature()
    {
        return temperature;
    }

    public float getPressure()
    {
        return pressure;
    }

    public float getHumidity()
    {
        return humidity;
    }

    public void dataChange()
    {
        notifyObserver();
    }

    //当数据有更新时，就调用setData
    public void setData(float temperature, float pressure, float humidity)
    {
        this.temperature = temperature;
        this.pressure = pressure;
        this.humidity = humidity;
        //调用dataChange,将最新的信息推送给接入方currentConditions
        dataChange();
    }

    @Override
    public void registerObserver(Observer o)
    {
        observers.add(o);
    }

    @Override
    public void remove(Observer o)
    {
        observers.remove(o);
    }

    @Override
    public void notifyObserver()
    {
        for (Observer observer : observers)
        {
            observer.update(this.temperature, this.pressure, this.humidity);
        }
    }
}
